/*
	Copyright 2007-2010 Petr Ohlidal

	This file is a part of GrobMaker

	GrobMaker is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	GrobMaker is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with GrobMaker.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "PegPosition.hpp"
#include "../Util/Vector.hpp"
#include "../OutputData.hpp"
#include "../Images/AreaFinder.hpp"
#include "../Images/Image.hpp"

namespace GrobMaker{

PegPosition PegPosition::parsePegPosFromImage(
		const fs::path& path,
		OutputData& outputData,
		const AreaFinder& pegFinder,
		const AreaFinder& pegGroundFinder)
{
	Image pegPic(path);
	Area blueArea, aquaArea;
	if(
		!pegPic.findArea(blueArea, pegFinder)
		|| !pegPic.findArea(aquaArea, pegGroundFinder))
	{
		std::stringstream err;
		err<<"parsePegPosFromImage(): Image '"<<path.string()<<"' is not a valid peg image";
		throw Exception(err);
	}
	// Align peg position to sprite output size
	Vector pegVec = blueArea.getCenter().scale(
		pegPic.getWidth(), pegPic.getHeight(),
		outputData.spritePicSize.w, outputData.spritePicSize.h);
	Vector pegGroundVec = aquaArea.getCenter().scale(
		pegPic.getWidth(), pegPic.getHeight(),
		outputData.spritePicSize.w, outputData.spritePicSize.h);
	return PegPosition( pegGroundVec.x, pegGroundVec.y, pegGroundVec.y-pegVec.y );
}

} // namespace GrobMaker
